package com.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import org.apache.commons.lang.time.DateFormatUtils;
import org.apache.commons.lang.time.DateUtils;

public class DateUtil {
	
	public final static String Date_Format_1 = "yyyy/MM/dd";
	public final static String Date_Format_2 = "yyyy-MM-dd";
//	public final static String Date_Format_3 = "yyyy/MM/dd";
//	public final static String Date_Format_4 = "yyyy/MM/dd";

	public static Date str2date(String str) {
		String[] parsePatterns = new String[]{Date_Format_1, Date_Format_2};
		try {
			return DateUtils.parseDate(str, parsePatterns);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
	}
	
//	public static Date nextDay(Date date) {
//		Calendar ca = Calendar.getInstance();
//		ca.setTime(date);
//		ca.add(Calendar.DAY_OF_MONTH, 1);
//		
//		return ca.getTime();
//	}
	
	public static Date today() {
		Calendar ca = Calendar.getInstance();
		ca.setTime(new Date());
		
		ca.set(Calendar.MILLISECOND, 0);
		ca.set(Calendar.SECOND, 0);
		ca.set(Calendar.MINUTE, 0);
		ca.set(Calendar.HOUR_OF_DAY, 0);
		
		return ca.getTime();
	}
	
//	public static Date add(Date date, int amount, int field) {
//		Calendar ca = Calendar.getInstance();
//		ca.setTime(date);
//		
//		ca.add(field, amount);
//		return ca.getTime();
//	}
//	
//	public static Date subtract(Date date, int amount, int field) {
//		Calendar ca = Calendar.getInstance();
//		ca.setTime(date);
//		
//		ca.add(field, 0-amount);
//		return ca.getTime();
//	}
	
	public static Date firstDayOfMonth(Date date) {
		return DateUtils.round(date, Calendar.MONTH);
	}
	
	public static Date mondayOfWeek(Date date) {
		Calendar ca = Calendar.getInstance();
		ca.setTime(date);
		
		ca.set(Calendar.MILLISECOND, 0);
		ca.set(Calendar.SECOND, 0);
		ca.set(Calendar.MINUTE, 0);
		ca.set(Calendar.HOUR_OF_DAY, 0);
		ca.set(Calendar.DAY_OF_WEEK, 2);
		return ca.getTime();
	}
	
	public static Date sundayOfWeek(Date date) {
		Calendar ca = Calendar.getInstance();
		ca.setTime(date);
		
		ca.set(Calendar.MILLISECOND, 0);
		ca.set(Calendar.SECOND, 0);
		ca.set(Calendar.MINUTE, 0);
		ca.set(Calendar.HOUR_OF_DAY, 0);
		ca.set(Calendar.DAY_OF_WEEK, 1);
		return ca.getTime();
	}
	
	
	public static void main(String[] a) {
		System.out.println(new Date());
		System.out.println(mondayOfWeek(new Date()));
	}
}
